﻿using System.Collections.Generic;
using System.Linq;

using Cinema.BL.Interfaces.DTOs;
using Cinema.BL.Interfaces.Managers;
using Cinema.Model.Interfaces;
using Cinema.Model.Entities;
using System;

namespace Cinema.BL.Managers
{
    public class UserManager : IUserManager
    {
        private IUnitOfWork _unitOfWork;

        public UserManager(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public UserDTO GetUserByUserName(string username)
        {
            var results = _unitOfWork.UserRepository.Read().Where(x => x.Username.Equals(username)).Select(x => new UserDTO()
            {
                Id = x.Id,
                Username = x.Username,
                Password = x.Password,
                Name = x.Name,
                Role = (x.Role == Model.Entities.Enums.Role.Employee) ? "employee" : "user" 
            });

            if (results.Count() > 0)
                return results.First();

            return null;
        }

        public UserDTO InsertUser(UserDTO user)
        {
            try
            {
                User insertedUser = _unitOfWork.UserRepository.Create(new User()
                {
                    Username = user.Username,
                    Password = user.Password,
                    Name = user.Name,
                    Role = (user.Role == "employee") ? Model.Entities.Enums.Role.Employee : Model.Entities.Enums.Role.User
                });
                _unitOfWork.SaveChanges();

                return new UserDTO()
                {
                    Id = insertedUser.Id,
                    Username = insertedUser.Username,
                    Password = insertedUser.Password,
                    Name = insertedUser.Name,
                    Role = (insertedUser.Role == Model.Entities.Enums.Role.Employee) ? "employee" : "user" 
                };
            }
            catch (Exception)
            {
                return null;
            }
        }

        public UserDTO UpdateUser(UserDTO user)
        {
            try
            {
                User updatedUser = _unitOfWork.UserRepository.Update(new User()
                {
                    Username = user.Username,
                    Id = user.Id,
                    Name = user.Name,
                    Password = user.Password,
                    Role = (user.Role == "employee") ? Model.Entities.Enums.Role.Employee : Model.Entities.Enums.Role.User
                });
                _unitOfWork.SaveChanges();

                return new UserDTO()
                {
                    Username = updatedUser.Username,
                    Name = updatedUser.Name,
                    Id = updatedUser.Id,
                    Password = updatedUser.Password,
                    Role = (updatedUser.Role == Model.Entities.Enums.Role.Employee) ? "employee" : "user" 
                };
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
